# -*- coding:utf-8 -*-
from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.table2Json_v2 import Table2Json


class InvoiceDao(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections_v2()

    def insertInvoice(self, info):
        """
        添加 invoice 信息
        :param info:
        :return:
        """
        userId = info['userId']
        invoiceType = info['invoiceType']
        invoiceContent = info['invoiceContent']
        invoiceHeader = info['invoiceHeader']
        invoiceStatus = info['invoiceStatua']
        invoiceRemark = info['invoiceRemark']

        sql = "insert into zzh_invoice (user_id,invoice_type, invoice_content,invoice_header,invoice_status ,invoice_remark) VALUES (%s, %s, %s, %s, %s, %s)"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [userId, invoiceType, invoiceContent, invoiceHeader, invoiceStatus, invoiceRemark])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def findInvoiceById(self, invocId):
        """
        根据ID查询 invoice 信息
        :param invocId:
        :return:
        """
        sql = "select * from zzh_invoice WHERE invoice_id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [invocId])
        jsons = None
        if rs:
            list0 = cur.fetchall()
            jsons = Table2Json.invoice_list_to_json(list0)
        cur.close()
        self.conn.close()
        return jsons

    def findInvoiceByCondition(self, **kwargs):
        """
        invoice 表的条件查询
        :param kwargs:
        :return:
        """
        userId = kwargs['userId']

        sql = "select * from zzh_invoice WHERE user_id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [userId])
        jsons = None
        if rs:
            list0 = cur.fetchall()
            jsons = Table2Json.invoice_list_to_json(list0)
        cur.close()
        self.conn.close()
        return jsons

    def deleteInvoice(self, idArray):
        """
        根据ID删除信息，可以传递一个或者多个ID，ID以数组形式传递
        :param idArray:
        :return:
        """
        sql = "delete from zzh_invoice WHERE invoice_id=%s"
        cur = self.conn.cursor()
        for idd in idArray:
            rs = cur.execute(sql, [idd])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def updateInvoice(self, info):
        """
        更新 invoice 表
        :param info:
        :return:
        """
        invoiceId = info['invoiceId']
        userId = info['userId']
        invoiceType = info['invoiceType']
        invoiceContent = info['invoiceContent']
        invoiceHeader = info['invoiceHeader']
        invoiceStatus = info['invoiceStatua']
        invoiceRemark = info['invoiceRemark']

        sql = "update zzh_invoice set user_id=%s,invoice_type=%s,invoice_content=%s,invoice_header=%s," \
              "invoice_status=%s,invoice_remark=%s " \
              " WHERE invoice_id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [userId,invoiceType,invoiceContent,invoiceHeader,invoiceStatus,invoiceRemark, invoiceId])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def selectInvoice(self,invoId):
        """
        根据ID查询，返回元组
        :param invoId:
        :return:
        """
        sql = "select * from zzh_invoice WHERE invoice_id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [invoId])
        arr = cur.fetchone()
        cur.close()
        self.conn.close()
        return arr